#!/bin/sh

#
# Test out the info function
#

. "$(cd "$(dirname "$0")" && pwd)/common.sh"

cat >$CONFIG <<EOF
file-resource 1.bin {
	host-path = "${TESTFILE_1K}"
}
file-resource 2.bin {
	host-path = "${TESTFILE_1K}"
}
file-resource 3.bin {
	host-path = "${TESTFILE_1K}"
}
file-resource 4.bin {
	host-path = "${TESTFILE_1K}"
}

task complete {
        on-resource 1.bin { info("1.bin") }
        on-resource 2.bin { info("2.bin") }
        on-resource 3.bin { info("3.bin") }
        on-resource 4.bin { info("4.bin") }
}
EOF

$FWUP_CREATE -c -f $CONFIG -o $FWFILE

# Try info() with numeric progress
$FWUP_APPLY -a -n -d $IMGFILE -i $FWFILE -t complete > $WORK/actual_output.txt
cat >$WORK/expected_output.txt <<EOF
0
fwup: 1.bin
fwup: 2.bin
fwup: 3.bin
fwup: 4.bin
100
EOF
diff -w $WORK/expected_output.txt $WORK/actual_output.txt

# Try info() with regular progress
$FWUP_APPLY -a -d $IMGFILE -i $FWFILE -t complete \
    | tr '\r' '.' \
    | tr '\033' '~' \
    | sed 's/\.$//g' \
    | grep -v '^Elapsed time' \
    > $WORK/actual_output.txt
cat >$WORK/expected_output.txt <<EOF
.  0% [                                    ].~[Kfwup: 1.bin
.~[Kfwup: 2.bin
.~[Kfwup: 3.bin
.~[Kfwup: 4.bin
.100% [====================================]
Success!
EOF
diff -w $WORK/expected_output.txt $WORK/actual_output.txt

# Try info() in quiet mode
$FWUP_APPLY -a -q -d $IMGFILE -i $FWFILE -t complete > $WORK/actual_output.txt
cat >$WORK/expected_output.txt <<EOF
fwup: 1.bin
fwup: 2.bin
fwup: 3.bin
fwup: 4.bin
EOF
diff -w $WORK/expected_output.txt $WORK/actual_output.txt

# Check that the verify logic works on this file
$FWUP_VERIFY -V -i $FWFILE
